export default defineNuxtPlugin(() => {
  if (process.server) return

  const observer = new IntersectionObserver(
    (entries) => {
      entries.forEach((entry) => {
        const el = entry.target as HTMLElement
        if (entry.isIntersecting) {
          el.classList.add('sa-in')
          observer.unobserve(el)
        }
      })
    },
    { root: null, rootMargin: '0px', threshold: 0.15 }
  )

  const scan = () => {
    document.querySelectorAll<HTMLElement>('[data-sa]')?.forEach((el) => {
      el.classList.add('sa')
      const delay = el.dataset.saDelay
      if (delay) el.style.transitionDelay = `${delay}ms`
      observer.observe(el)
    })
  }

  window.addEventListener('DOMContentLoaded', scan)
  scan()
})


